package cn.wolfcode.service.impl;

import cn.wolfcode.cache.service.IUserInfoRedisService;
import cn.wolfcode.domain.UserInfo;
import cn.wolfcode.exception.LogicException;
import cn.wolfcode.repository.UserInfoRepository;
import cn.wolfcode.service.IUserInfoService;
import cn.wolfcode.util.AssertUtil;
import cn.wolfcode.util.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {



    @Autowired
    private UserInfoRepository userInfoRepository;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Override
    public void save(UserInfo userInfo) {
        userInfo.setId(null);
        userInfoRepository.save(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoRepository.save(userInfo);
    }

    @Override
    public void delete(String id) {
        userInfoRepository.deleteById(id);
    }

    @Override
    public UserInfo get(String id) {
        return userInfoRepository.findById(id).orElse(null);

    }

    @Override
    public List<UserInfo> findAll() {
        return userInfoRepository.findAll();
    }

    @Override
    public boolean checkPhone(String phone) {
        UserInfo userInfo = userInfoRepository.findByPhone(phone);
        return userInfo != null;
    }

    @Override
    public boolean sendVerifyCode(String phone, String smsUrl, String appKey) {
        //创建验证码,编辑短信
        String code = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4);
        StringBuilder sb = new StringBuilder(80);
        sb.append("您注册的验证码是").append(code).append(",请在").append(Consts.VERIFY_CODE_VAI_TIME).append("分钟内使用");
        //发送短信
        System.out.println(sb.toString());


//        RestTemplate template = new RestTemplate();
//        //参数1:请求url url中有占位符   参数2:返回值类型  参数3:请求需要参数
//        String s = template.getForObject(smsUrl, String.class, phone, "【叩丁狼】" + sb.toString(), appKey);
//        System.out.println(s);
//        if(s.contains("Success")){
//            throw new LogicException("短信发送失败");
//        }


        //将验证码缓存到redis中
        userInfoRedisService.sendVerifyCode(phone, code);
        return false;
    }

    @Override
    public void regist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        //判断传入参数是否为null
        AssertUtil.hasLength(phone,"手机号码不能为空");
        AssertUtil.hasLength(nickname,"昵称不能为空");
        AssertUtil.hasLength(password,"密码不能为空");
        AssertUtil.hasLength(rpassword,"确认密码不能为空");
        AssertUtil.hasLength(verifyCode,"验证码不能为空");

        //判断两次输入密码是否一致
        AssertUtil.isEquals(password, rpassword, "两次输入密码不一致");

        //判断手机号码是否符合格式@TODO


        //判断手机号码是否唯一
        if(this.checkPhone(phone)){
            throw new LogicException("该手机号码已被注册");
        }

        //判断验证码是否正确
        String code = userInfoRedisService.getVerifyCode(phone);

        if(code == null || !code.equalsIgnoreCase(verifyCode)){
            throw new LogicException("验证码错误");
        }


        //注册
        UserInfo userInfo = new UserInfo();
        userInfo.setCity("");
        userInfo.setEmail("");
        userInfo.setNickname(nickname);
        userInfo.setPhone(phone);
        userInfo.setPassword(password);
        userInfo.setLevel(1);
        userInfo.setEmail("");
        userInfo.setHeadImgUrl("/images/default.jpg");
        userInfo.setInfo("");
        userInfo.setState(userInfo.STATE_NORMAL);
        this.save(userInfo);

    }

    @Override
    public UserInfo login(String username, String password) {
        //1.password之前加密,此处需要加密
        //2.此处需要进行参数判断为null判断
        UserInfo user = userInfoRepository.findByPhone(username);
        if(user == null || !password.equals(user.getPassword())){
            throw new LogicException("账号或密码错误");
        }
        return user;
    }

    @Override
    public List<UserInfo> getByCity(String city) {
        return userInfoRepository.findByCity(city);
    }


}
